package team.niit.dao.impl;

import cn.hutool.core.convert.Convert;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;
import team.niit.dao.StudentDAO;
import team.niit.entity.StudentEntity;
import team.niit.util.SessionFactoryUtil;

import java.util.List;

/**
 * @Author: HAPPY
 * @Project_name: JSF
 * @Package_name: team.niit.dao.impl
 * @Date: 2021/10/26 20:07
 * Explain:
 */
public class StudentDAOImpl implements StudentDAO {

    /**
     * 学生登录验证
     *
     * @param id       学号
     * @param password 密码
     * @return
     */
    @Override
    public Object login(String id, String password) {
        Session session = SessionFactoryUtil.getSessionFactory().openSession();
        String hql = "from team.niit.entity.StudentEntity student where student.id = :id and student.password= :password";
        Query query = session.createQuery(hql);
        query.setParameter("id", id);
        query.setParameter("password", password);
        if (query.list().size() > 0) {
            return query.list().get(0);
        } else {
            return null;
        }
    }


    @Override
    public boolean retrievePassword(String id, String password, String phone) {
        Session session = SessionFactoryUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        String hql = "update from team.niit.entity.StudentEntity student set student.password=:password where student.id = :id and student.phone= :phone";
        Query query = session.createQuery(hql);
        query.setParameter("password", password);
        query.setParameter("id", id);
        query.setParameter("phone", phone);
        int num = query.executeUpdate();
        tx.commit();
        if (num > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean modifyPassword(String id, String oldPassword, String newPassword) {
        Session session = SessionFactoryUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        String hql = "update from team.niit.entity.StudentEntity student set student.password=:newPassword where student.id = :id and student.password= :oldPassword";
        Query query = session.createQuery(hql);
        query.setParameter("newPassword", newPassword);
        query.setParameter("id", id);
        query.setParameter("oldPassword", oldPassword);
        int num = query.executeUpdate();
        tx.commit();
        if (num > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public StudentEntity selectStudentById(String id) {
        return null;
    }

    @Override
    public StudentEntity updateStudentById(String id, StudentEntity student) {
        return null;
    }

    @Override
    public List<StudentEntity> selectStudentList() {
        Session session = SessionFactoryUtil.getSessionFactory().openSession();
        String hql = "from team.niit.entity.StudentEntity";
        Query query = session.createQuery(hql);
        return query.list();
    }

    @Override
    public List<StudentEntity> pagingQuery(int size, int pageNumber) {
        Session session = SessionFactoryUtil.getSessionFactory().openSession();
        Query query = session.createQuery("from team.niit.entity.StudentEntity");
        int resSize = query.list().size();
        int firstRes;
        if (pageNumber > 0) {
            firstRes = size * pageNumber;
        } else {
            firstRes = 0;
        }
        // 如果当前查询条数大于总条数
        if (size > resSize) {
            firstRes = 0;
            size = resSize;
        } else {
            // 如果当前的开始位置加上要查询的数量大于总的
            if (size + firstRes > resSize) {
                size = resSize - firstRes;
            }
        }
        query.setFirstResult(firstRes);
        query.setMaxResults(size);
        return query.list();
    }

    @Override
    public int count() {
        Session session = SessionFactoryUtil.getSessionFactory().openSession();
        SQLQuery sqlQuery = session.createSQLQuery("select count(*) from student");
        int res = Convert.toInt(sqlQuery.list().get(0));
        return res;
    }

    /**
     * 删除学生
     *
     * @param studentId
     * @return
     */
    @Override
    public boolean dropStudent(String studentId) {
        Session session = SessionFactoryUtil.getSessionFactory().openSession();
        Transaction transaction = session.beginTransaction();
        String sql = "delete from student where id = ?";
        SQLQuery query = session.createSQLQuery(sql);
        query.setParameter(1, studentId);
        query.executeUpdate();
        transaction.commit();
        return true;
    }

    @Override
    public boolean addStudent(StudentEntity student) {
        Session session = SessionFactoryUtil.getSessionFactory().openSession();
        Transaction transaction = session.beginTransaction();
        session.save(student);
        transaction.commit();
        return true;
    }

    @Override
    public boolean initPassword(String id, String password) {
        Session session = SessionFactoryUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        String hql = "update from team.niit.entity.StudentEntity student set student.password=:password where student.id = :id";
        Query query = session.createQuery(hql);
        query.setParameter("id", id);
        query.setParameter("password", password);
        int num = query.executeUpdate();
        tx.commit();
        if (num > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public StudentEntity modifyStudentById(StudentEntity student) {
        Session session = SessionFactoryUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        String hql = "update from team.niit.entity.StudentEntity student " +
                "set student.gender=:gender,student.birth=:birth,student.phone=:phone,student.classes=:classes  where student.id = :id";
        Query query = session.createQuery(hql);
        query.setParameter("gender", student.getGender());
        query.setParameter("birth", student.getBirth());
        query.setParameter("classes", student.getClasses());
        query.setParameter("phone", student.getPhone());
        query.setParameter("id", student.getId());
        query.executeUpdate();
        tx.commit();
        return student;
    }
}
